package org.fujene.communicate.hash;

import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.TreeMap;

import org.fujene.communicate.node.Node;

public final class HashCompareTool{
	private static SortedSet<Node> nodeList;
	private static Node myNode;
	private static int replication;

	private HashCompareTool(){/* No constructor */}

	public static void registerLists(int repli, Node ownNode, SortedSet<Node> node){
		nodeList = node;
		myNode = ownNode;
		replication = repli;
	}

	public static boolean existsInMyNode(long target){
		TreeMap<Long, Node> tempList = new TreeMap<Long, Node>();
		
		for(Node eachNode: nodeList)
			tempList.put(eachNode.getAttributeHash(target), eachNode);
		
		Entry<Long, Node> entries = tempList.ceilingEntry(Long.valueOf(target));
		if(entries == null) entries = tempList.firstEntry();
		for(int i = 0; i < replication; i++){
			if(entries.getValue() == myNode) return true;
			entries = tempList.higherEntry(entries.getKey());
			if(entries == null) entries = tempList.firstEntry();
		}
		
		return false;
	}
}
